(library (file-reader)
  (export process-file
          get-string-from-file)
  (import
    (except (rnrs base) let-values)
    (only (guile)
          lambda* λ
          error
          display
          call-with-input-file
          set-port-encoding!)
    ;; Guile modules
    ;; alist->hash-table
    ;; (prefix (ice-9 hash-table) ice9-hash-table:)
    ;; Guile exception handling
    (ice-9 exceptions)
    ;; for bytevector operations
    (ice-9 binary-ports)
    (ice-9 textual-ports)
    (ice-9 rdelim)  ; for the eof-object? binding
    ;; SRFIs
    ;; hash tables
    ;; (prefix (srfi srfi-69) srfi-69:)
    ;; ;; receive form
    ;; (prefix (srfi srfi-8) srfi-8:)
    ;; ;; let-values
    ;; (prefix (srfi srfi-11) srfi-11:)
    ;; ;; list utils
    ;; (prefix (srfi srfi-1) srfi-1:)
    (prefix (logging) log:)))


(define get-string-from-file
  (lambda* (file-path #:key (encoding "UTF-8"))
    (call-with-input-file file-path
      (λ (port)
        (set-port-encoding! port encoding)
        (get-string-all port)))))


(define process-file
  (λ (loc proc)
    ;; call-with-input-file automatically closes the port
    ;; when the given procedure returns.
    (call-with-input-file loc
      (λ (fport)
        (proc fport))
      #:guess-encoding #f
      #:binary #t)))


;; get-string-all


;; (call-with-output-file file-path
;;       (lambda (port)
;;         (rnrs:assert (list? scm-output))
;;         (set-port-encoding! port encoding)
;;         (scm->dsv (stringify* scm-output)
;;                   port
;;                   delimiter
;;                   #:format format)))

;; flushing the buffers of ports:
;; force-output
;; close-port


;; setvbuf port mode [size]
;; mode: 'none 'line 'block (with size)
